package com.promis.crypto;

import java.io.IOException;
import java.security.Key;
import javax.crypto.Cipher;

public class KeyCipher {
	
	protected String keyAlgorithm;
	protected String cipherAlgorithm;
	
	public KeyCipher(String keyAlgorithm, String cipherAlgorithm) {
		this.keyAlgorithm = keyAlgorithm;
		this.cipherAlgorithm = cipherAlgorithm;
	}

	protected byte[] cipher(int cipherMode, byte[] data, Key key) throws Exception {
		Cipher cipher = Cipher.getInstance(cipherAlgorithm);  
		cipher.init(cipherMode, key);  
		return cipher.doFinal(data);
	}
	
	public static String toKey(byte[] key) {
		return BASE64Coder.encryptBASE64(key);
	}
	
	public static byte[] toKey(String key) throws IOException {
		return BASE64Coder.decryptBASE64(key);
	}
}
